package com.demo.config;

import com.demo.controller.BlogController;
import com.demo.controller.IndexController;
import com.demo.controller.PointController;
import com.demo.controller.RoleController;
import com.demo.controller.SubjectController;
import com.demo.controller.UploadController;
import com.demo.controller.UserController;
import com.demo.interceptor.AccessTokenInterceptor;
import com.demo.interceptor.ExceptionInterceptor;
import com.demo.model._MappingKit;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.kit.PathKit;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.plugin.redis.RedisPlugin;
import com.jfinal.template.Engine;

public class BaseConfig extends JFinalConfig {

	@Override
	public void configConstant(Constants me) {
		PropKit.use("config.txt");
		loadPropertyFile("log4j.properties");
		me.setEncoding("UTF-8");
//		 me.setDevMode(PropKit.getBoolean("devMode", false)); // true 打印url,method等日志
		me.setBaseUploadPath("../"); // 默认上传路径是 PathKit.getWebRoot() + "/upload"
		System.setProperty("log4jdir", PathKit.getWebRootPath());
	}

	@Override
	public void configEngine(Engine me) {
		me.addSharedFunction("/common/_layout.html");
		me.addSharedFunction("/common/_paginate.html");
	}

	public static DruidPlugin createDruidPlugin() {
		return new DruidPlugin(PropKit.get("driver").trim(), PropKit
				.get("user").trim(), PropKit.get("password").trim());
	}

	@Override
	public void configHandler(Handlers me) {

	}

	@Override
	public void configInterceptor(Interceptors me) {
//		me.addGlobalActionInterceptor(new AccessTokenInterceptor());
		me.addGlobalActionInterceptor(new ExceptionInterceptor());
		// 需要开启事务的action
		// String[] actionKeys = { "/upload/add" };
		// me.add(new TxByActionKeys(actionKeys));
	}

	@Override
	public void configPlugin(Plugins me) {
		//是否需要redis
//		RedisPlugin redisPlugin = new RedisPlugin("user", "localhost");
//		me.add(redisPlugin);
		// 配置C3p0数据库连接池插件
		DruidPlugin druidPlugin = createDruidPlugin();
		me.add(druidPlugin);
		// 配置ActiveRecord插件
		ActiveRecordPlugin arp = new ActiveRecordPlugin(druidPlugin);
		// 是否打印sql
		arp.setShowSql(true);
		// 设置发sql 文件存放的基础路径
		arp.setBaseSqlTemplatePath(PathKit.getRootClassPath() + "\\sql");
		arp.addSqlTemplate("_all.sql");
		// 所有映射在 MappingKit 中自动化搞定
		_MappingKit.mapping(arp);
		me.add(arp);
	}

	@Override
	public void configRoute(Routes me) {
		me.add("/", IndexController.class); 
//		me.add("/", IndexController.class, "index"); // 第三个参数为该Controller的视图存放路径
		me.add("/blog", BlogController.class); // 第三个参数省略时默认与第一个参数值相同，在此即为"/blog"
		me.add("/user", UserController.class);
		me.add("/subject", SubjectController.class);
		me.add("/point", PointController.class);
		me.add("/upload", UploadController.class);
		me.add("/role", RoleController.class);
	}
}
